package com.hainatong.shiphelper.mainhome.fragment;


import android.content.Intent;
import android.content.res.Configuration;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.databinding.DataBindingUtil;

import com.base.common.app.LoginUtils;
import com.base.common.view.base.BaseFragment;
import com.base.common.viewmodel.BaseViewModel;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.SPUtils;
import com.hainatong.shiphelper.Constants;
import com.hainatong.shiphelper.GlobalConfig;
import com.hainatong.shiphelper.R;
import com.hainatong.shiphelper.databinding.FragmentUserBinding;
import com.hainatong.shiphelper.login.view.DialogActivity;
import com.hainatong.shiphelper.login.view.LoginActivity;
import com.hainatong.shiphelper.mainhome.ComFragmentActivity;
import com.hainatong.shiphelper.mainhome.MainHomeActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;


public class UserFragment extends BaseFragment<FragmentUserBinding, BaseViewModel> {

    @Override
    protected FragmentUserBinding initDataBinding(LayoutInflater inflater, ViewGroup container) {
        return DataBindingUtil.inflate(inflater, R.layout.fragment_user, container, false);
    }


    @Override
    public void initView() {

        binding.rlUserInfo.setOnClickListener(v -> {

            Bundle bundle = new Bundle();
            bundle.putString(Constants.FRAGMNET_TYPE, Constants.USER_INFO);
            ComFragmentActivity.start(getActivity(), bundle);

        });

        binding.rlSetPwd.setOnClickListener(v -> {

            Bundle bundle = new Bundle();
            bundle.putString(Constants.FRAGMNET_TYPE, Constants.SET_PWD);
            ComFragmentActivity.start(getActivity(), bundle);

        });
        binding.rlHandPwd.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constants.FRAGMNET_TYPE, Constants.HAND_PWD);
            ComFragmentActivity.start(getActivity(), bundle);
        });

        binding.rlChangeLanguage.setOnClickListener(v -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constants.FRAGMNET_TYPE, Constants.CHANGE_LANGUAGE);
            ComFragmentActivity.start(getActivity(), bundle);
        });

        binding.rlSignOut.setOnClickListener(v -> {

            LoginUtils.loginOut();
            LoginActivity.newLoginStart( getActivity() );

            startActivity(new Intent(getActivity(), DialogActivity.class));
            getActivity().finish();
        });

        boolean isNight = false;

        if ((getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES) {
            //夜间模式时
            isNight = true;
            binding.btnSwitchTheme.setToggleOn();
        } else {
            binding.btnSwitchTheme.setToggleOff();
        }
        //启用动画
        binding.btnSwitchTheme.setAnimate(true);

        // 切换主题
        binding.btnSwitchTheme.setOnToggleChanged((on) -> {

            switchNightMode(on);
//            Observable.timer(500, TimeUnit.MILLISECONDS)
//                    .observeOn(AndroidSchedulers.mainThread())
//                    .subscribe((t) -> {
//                        switchNightMode(on);
//                    });

        });

    }

    /**
     * 日夜间模式切换
     */
    private void switchNightMode(boolean isNight) {
        //boolean isNight = SPUtils.getInstance().getBoolean("SWITCH_MODE_KEY", false);
        if (isNight) {
            // 夜间模式
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
            SPUtils.getInstance().put("SWITCH_MODE_KEY", true);
        } else {
            // 日间模式
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
            SPUtils.getInstance().put("SWITCH_MODE_KEY", false);
        }
        MainHomeActivity.reStart(getActivity());
//        getWindow().setWindowAnimations(R.style.WindowAnimationFadeInOut);
//        AppCompatActivity activity = (AppCompatActivity) getActivity();

//        activity.recreate();
    }

    @Override
    public void onResume()
    {
        super.onResume();
        if(ObjectUtils.isNotEmpty(GlobalConfig.getInstance().getUserInfo()))
        {
            //有 用户 数据时
            binding.tvUserName.setText( GlobalConfig.getInstance().getUserInfo().getUserName() );

            String workType = GlobalConfig.getInstance().getUserInfo().getShip_name()+" | "+ GlobalConfig.getInstance().getUserInfo().getRoleName() ;

            binding.tvUserWorkType.setText( workType );

        }

    }
}
